package com.jcfk.system.service;

import com.jcfk.system.domain.dto.AnnexesFileDTO;
import com.jcfk.system.domain.po.OaKeyworkDailyDetail;
import com.jcfk.system.domain.po.SysAnnexesFile;
import com.jcfk.system.domain.query.OaKeyworkDailyDetailQuery;
import com.jcfk.system.domain.dto.OaKeyworkDailyDetailDTO;
import com.jcfk.system.dao.OaKeyworkDailyDetailDao;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import com.jcfk.common.core.utils.IdUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.Date;

/**
 * 日报实际
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-04-01
 */
@Service
public class OaKeyworkDailyDetailService extends BaseService<OaKeyworkDailyDetailDao, OaKeyworkDailyDetail> {


    @Autowired
    private SysAnnexesFileService sysAnnexesFileService;

    /**
     * OaKeyworkDailyDetail分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<OaKeyworkDailyDetailDTO> queryByPage(DataPage<OaKeyworkDailyDetailDTO> page, OaKeyworkDailyDetailQuery query) {
        // 分页查询
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }
//

    /**
     * 判断OaKeyworkDailyDetail是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(OaKeyworkDailyDetailDTO dto) {
        QueryWrapper<OaKeyworkDailyDetail> queryWrapper = new QueryWrapper<>();

        return RetResult.ok();
    }

    /**
     * 插入日报实际
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(OaKeyworkDailyDetailDTO dto) {

        // 保存
        OaKeyworkDailyDetail item = new OaKeyworkDailyDetail();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新日报实际
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(OaKeyworkDailyDetailDTO dto) {

        // 保存
        OaKeyworkDailyDetail item = new OaKeyworkDailyDetail();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);


        super.updateById(item);
    }


    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(OaKeyworkDailyDetail item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

    }

    /**
     * 根据主键获取日报实际信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public OaKeyworkDailyDetailDTO get(String id) {
        // 查询数据
        OaKeyworkDailyDetail item = this.baseMapper.selectById(id);
        OaKeyworkDailyDetailDTO dto = new OaKeyworkDailyDetailDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }

    /**
     * 日报实际批量保存
     *
     * @param dtos
     * @return StandardCodeDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(List<OaKeyworkDailyDetailDTO> dtos, String dailyId) {
        List<OaKeyworkDailyDetail> insertList = new ArrayList<>();
        List<OaKeyworkDailyDetail> updateList = new ArrayList<>();
        List<String> notDeleteIds = new ArrayList<>();

        List<String> fileIds = new ArrayList<>();

        List<SysAnnexesFile> files = new ArrayList<>();

        for (OaKeyworkDailyDetailDTO dto : dtos) {
            OaKeyworkDailyDetail item = new OaKeyworkDailyDetail();
            BeanUtils.copyProperties(dto, item);
            this.setDefaultValue(item);
            if (!StringUtils.isEmpty(dto.getId())) {
                notDeleteIds.add(dto.getId());
                BeanUtils.copyProperties(dto, item);
                updateList.add(item);

            } else {
                item.setId(IdUtils.newId());
                insertList.add(item);
            }

            if (dto.getFile() != null && dto.getFile().getPath() != null && !dto.getFile().getPath().equals("")) {
                SysAnnexesFile file = new SysAnnexesFile();
                BeanUtils.copyProperties(dto.getFile(), file);
                file.setOwnerId(item.getId());
                file.setOwnerType("daily_detail_file");
                file.setCreatedUserId(SecurityUtils.getUserId());
                file.setCreatedUserName(SecurityUtils.getUserName());
                file.setCreateTime(new Date());
                file.setLastUpdatedUserId(SecurityUtils.getUserId());
                file.setLastUpdatedUserName(SecurityUtils.getUserName());
                file.setLastUpdateTime(new Date());
                files.add(file);
            }


            fileIds.add(item.getId());
        }

        // 删除
        QueryWrapper<OaKeyworkDailyDetail> queryWrapper = new QueryWrapper<>();
        if (notDeleteIds.size() > 0) queryWrapper.notIn("id", notDeleteIds);
        queryWrapper.lambda().eq(OaKeyworkDailyDetail::getDailyId, dailyId);
        this.remove(queryWrapper);

        // 保存新增
        if (insertList.size() > 0) this.saveBatch(insertList);

        // 保存更新
        if (updateList.size() > 0) this.updateBigBatch(updateList);

        if (fileIds.size() != 0) {
            QueryWrapper<SysAnnexesFile> fileQueryWrapper = new QueryWrapper<>();
            fileQueryWrapper.lambda().eq(SysAnnexesFile::getOwnerType, "daily_detail_file");
            fileQueryWrapper.lambda().in(SysAnnexesFile::getOwnerId, fileIds);
            sysAnnexesFileService.remove(fileQueryWrapper);
        }

        if (files.size() > 0) sysAnnexesFileService.saveBatch(files);

    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<OaKeyworkDailyDetailDTO>
     */
    public List<OaKeyworkDailyDetailDTO> getList(OaKeyworkDailyDetailQuery query) {
        // 查询条件
        QueryWrapper<OaKeyworkDailyDetail> qw = new QueryWrapper<>();
        // 查询数据
        List<OaKeyworkDailyDetail> list = super.list(qw);

        return convertList(list, OaKeyworkDailyDetailDTO.class);
    }
}

